//
//  WLTabBar.swift
//  weibo
//
//  Created by 刁 海瑞 on 16/8/8.
//  Copyright © 2016年 毋磊. All rights reserved.
//

import UIKit

class WLTabBar: UITabBar {
//    定义闭包
    var composeButtonClosure:(()->())?
   
//    Mark:懒加载
    private lazy var composeButton: UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: "composeButtonAction", forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
//        指定大小
        btn.sizeToFit()
        
    
        return btn
    }()
    
//    手写的方式去创建对象
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setUpUI()
    }
//    添加点击事件
    @objc private func composeButtonAction() {
        
//       执行闭包
      composeButtonClosure?()
        
    }
    
    
//    添加视图
    private func setUpUI() {
        
        addSubview(composeButton)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        composeButton.centerX = width * 0.5
        composeButton.centerY = height * 0.5
        
//        计算按钮的宽度
        let itemWidth = width / 5
        
        var index = 0
        
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
//                设置系统按钮的宽度
                value.width = itemWidth
                
//                指定系统的坐标系
                value.x = CGFloat(index) * itemWidth
                
                index++
                
                if index == 2{
                    
                    index++
                }
                
                
            }
            
        }
        
    }
    
}
